var path = $('body').attr('ctxPath');
//重置
$('#move_reset').on('click',function(){
	$('#moveUI .query.row :input').val("");
	$("#move_report_export").removeAttr("disabled");
	$("#move_search").click();
});
$('#rule_reset').on('click',function(){
	$('#ruleUI .query.row :input').val("");
	$("#rule_report_export").removeAttr("disabled");
	$("#rule_search").click();
});

//控制报表导出按钮
$('#moveUI .query.row :input').on('input propertychange', function() { 
	$("#move_report_export").attr("disabled","disabled");
});
$('#ruleUI .query.row :input').on('input propertychange', function() { 
	$("#rule_report_export").attr("disabled","disabled");
});
$('#move_search').on('click', function() { 
	$("#move_report_export").removeAttr("disabled");
});
$('#rule_search').on('click', function() { 
	$("#rule_report_export").removeAttr("disabled");
});

//表格刷新
$('section ul li:first').on('click',function(){
	listui.refresh("moveUI");//刷新
});
$('section ul li:last').on('click',function(){
	listui.refresh("ruleUI");//刷新
});
//数据表格操作按钮
function handleOpRow(tr, obj, container) {
	if(container.attr('id')=='moveUI'){
		$.post(path+'/report/material/photos/number/'+obj.id+'?t='+new Date().getTime(),function(data){
			if(data.state){
				tr.append($('<td/>').addClass('op').append($('<a/>', {
					"href" : "javascript:;",
					"data-id" : obj.id
				}).addClass('Tphotos').text("查看照片 ("+data.number+") 张")));
			}else{
				tr.append($('<td/>').addClass('op').append($('<span/>', {
					"data-id" : obj.id
				}).addClass('no_photos').text('无照片')));
			}
		}).error(function(){
			layer.alert("网络异常,请刷新页面再试");
		});
	}else if(container.attr('id')=='ruleUI'){
		$.post(path+'/report/rule/photos/number/'+obj.id+'?t='+new Date().getTime(),function(data){
			if(data.state){
				tr.append($('<td/>').addClass('op').append($('<a/>', {
					"href" : "javascript:;",
					"data-id" : obj.id
				}).addClass('photos').text("查看照片 ("+data.number+") 张")));
			}else{
				tr.append($('<td/>').addClass('op').append($('<span/>', {
					"data-id" : obj.id
				}).addClass('no_photos').text('无照片')));
			}
		}).error(function(){
			layer.alert("网络异常,请刷新页面再试");
		});
	}
}

$('section').on('click','.photos',function(){
	$("#photos_show").empty();
	var id=$(this).attr('data-id');
	$.post(path+'/report/rule/photos/'+id+'?t='+new Date().getTime(),function(data){
		if(data.state){
			var photoList=data.piclist;
			for(var i = 0; i < photoList.length; i++){
				$("#photos_show").append("<img src='"+photoList[i]+"'><hr>");
				}
			$('#photo_Modal').modal({show:true});
		}else{
		  layer.msg(data.msg);
		}
	}).error(function(){
		 layer.alert("网络异常,请刷新页面再试");
	});
	
});

$('section').on('click','.Tphotos',function(){
	$("#photos_show").empty();
	var id=$(this).attr('data-id');
	$.post(path+'/report/material/photos/'+id+'?t='+new Date().getTime(),function(data){
		if(data.state){
			var photoList=data.piclist;
			for(var i = 0; i < photoList.length; i++){
				$("#photos_show").append("<img src='"+photoList[i]+"'><hr>");
			}
			$('#photo_Modal').modal({show:true});
		}else{
			layer.msg(data.msg);
		}
	}).error(function(){
		layer.alert("网络异常,请刷新页面再试");
	});
	
});



//自定义表格信息
function handleCellValue(tfNotUnderline, cellVal, obj, container) {// (列名，单元格值，行对象)
	if(container.attr('id')=='moveUI'){
	  if(tfNotUnderline=="materielVolume") {
			if (cellVal != null||cellVal !='') {
				var val=Number(cellVal)/Number(1000000);
				return val.toFixed(3);
			} else {
				return;
			}
		}else if(tfNotUnderline=="bizType"){
			if(cellVal==10){
				return '装货';
			}else if(cellVal==20){
				return '卸货';
			}
		}else{
			return cellVal;
		}
	}else if(container.attr('id')=='ruleUI'){
		if(tfNotUnderline=="volume") {
			if (cellVal != null||cellVal !='') {
				var val=Number(cellVal)/Number(1000000);
				return val.toFixed(3);
			} else {
				return;
			}
		}else if(tfNotUnderline=='isException'){
			if(cellVal=='0'){
				return '正常';
			}else{
				return '异常';
			}
		}
		return cellVal;
	}
}
//货物移动报表
$('#move_report_export').on('click',function(){
	var json= {};
	json.mainInfo={};
	json.dataInfo=new Array();
	var data=new Array();
	//获取报表主要信息,即搜索条件
	json.mainInfo.materielCode=$("#materiel_code").val();
	json.mainInfo.materielName=$("#materiel_name").val();
	json.mainInfo.projectName=$("#project_name").val();
	json.mainInfo.planCode=$("#plan_code").val();
	json.mainInfo.containerName=$("#container_name").val();
	json.mainInfo.addressName=$("#address_name").val();
	json.mainInfo.userName=$("#user_name").val();
	//获取列表数据
	for(var i = 0; i <$("#moveUI table").find("tr").length; i++){
		var list = new Array();
		var len=$('#moveUI table>thead>tr').children('th').length;
		if(i==0){
			$('#moveUI table>thead>tr').children('th').each(function(j){ 
				//!=0去掉序号
				if(j!=0&&j!=(len-1)){
					list.push($(this).text().trim());
				}else if(j==(len-1)){
					list.push('相关照片');
				}
		    });
			data.push(list);
		}else{
			$('#moveUI table>tbody>tr:eq('+(i-1)+')').children('td').each(function(j){  
				if(j!=0&&j!=(len-1)){
					list.push(""+$(this).text().trim());
				}else if(j==(len-1)){
					if($(this).text()=='无照片'){
						list.push('notphoto');
					}else{
						list.push($(this).children('a').attr('data-id'));
					}
				}
			});
			data.push(list);
		}
	}
	json.dataInfo=data;
	deleteEmptyProperty(json);
	location.href= path+'/report/material/export?json='+encodeURIComponent(JSON.stringify(json));
	
	/*$.ajax({  
        url : path+'/report/material/export?t='+new Date().getTime(),
        type : "POST",  
        datatype:"json",  
        contentType: "application/json; charset=utf-8",  
        data : JSON.stringify(json),  
        success : function(data) {  
            if (data.stats) {  
              //location.href= $('body').attr('ctxPath')+'/materiel/download?fileName=物料模板.xls';
            }  
        },  
        error : function(data) {  
        	 layer.alert("网络异常,请刷新页面再试");
        }  
    }); */
	
});
//货物打尺报表
$('#rule_report_export').on('click',function(){
	var json= {};
	json.mainInfo={};
	json.dataInfo=new Array();
	var data=new Array();
	//获取报表主要信息,即搜索条件
	json.mainInfo.materielCode=$("#resource_code").val();
	json.mainInfo.materielName=$("#resource_name").val();
	json.mainInfo.positionName=$("#position_name").val();
	json.mainInfo.description=$("#description").val();
	json.mainInfo.isException=$("#is_exception").val();
	json.mainInfo.userName=$("#operator").val();
	//获取列表数据
	for(var i = 0; i <$("#ruleUI table").find("tr").length; i++){
		var list = new Array();
		var len=$('#ruleUI table>thead>tr').children('th').length;
		if(i==0){
			$('#ruleUI table>thead>tr').children('th').each(function(j){ 
				//!=0去掉序号
				if(j!=0&&j!=(len-1)){
					list.push($(this).text().trim());
				}else if(j==(len-1)){
					list.push('相关照片');
				}
		    });
			data.push(list);
		}else{
			$('#ruleUI table>tbody>tr:eq('+(i-1)+')').children('td').each(function(j){  
				if(j!=0&&j!=(len-1)){
					list.push(""+$(this).text().trim());
				}else if(j==(len-1)){
					if($(this).text()=='无照片'){
						list.push('notphoto');
					}else{
						list.push($(this).children('a').attr('data-id'));
					}
				}
			});
			data.push(list);
		}
	}
	json.dataInfo=data;
	deleteEmptyProperty(json);
	location.href= path+'/report/rule/export?json='+encodeURIComponent(JSON.stringify(json));
});


//删除Json中的空属性(null、undefined)
function deleteEmptyProperty(object){
 for (var i in object) {
	var value = object[i];
	if (typeof value === 'object') {
		if (Array.isArray(value)) {
			if (value.length == 0) {
			delete object[i];
			continue;
			}
		}
		deleteEmptyProperty(value);
		if (isEmpty(value)) {
			delete object[i];
		}
	} else {
		if (value === null || value === undefined) {
		delete object[i];
		}
	}
 }
}

function isEmpty(object) {
   for (var name in object) {
	return false;
   }
	return true;
}
